#Matan Keidar 066500992
#Eliahu Khalastchi 040854705

from Corpus import *
from BigramCorpus import *
import pickle
from Backoff import * 

debug_print = True

#File Names
train90 = "train90.txt"
train10 = "train10.txt"
train = "train.txt"
test = "test.txt"
test100 = "test_splitted.txt"
test0 = "dummy.txt"
conditionMap = "conditionMapping.txt"

def DEBUG(str):
    '''
    debug print in current module
    '''
    if debug_print:
        print "DEBUG:",str

class ModelUtils(object):
    '''
    Utilities module
    '''


    def __init__(self):
        '''
        Constructor
        '''
        #Splitting input test file
        totalLength, length90, length10 = self.splitFile(train, 0.9, train90,train10)
        DEBUG(("\tTotal words in train.txt =" , totalLength))
        DEBUG(("\tord count in train90.txt file =", length90)) 
        DEBUG(("\tWord count in train10.txt file =", length10)) 
          
        #Write test file line by line  
        self.splitFile(test, 1, test100, test0)
          
        #Creating corpuses from splitted file  
        DEBUG("creating corpuses")
        self.createCorpuses(train90)
        DEBUG("done")
        
        #create dic optimization
        DEBUG("creating dic")
        dic=self.createConditioningDic()
        DEBUG("done")
        
        #create backoff model
        DEBUG("creating backoff")
        self.backoff = Backoff(self.biCorpus, self.uniCorpus, dic)
        
        DEBUG("done")
    
    
    def getBackoffModel(self):
        '''
        Getter of the self generated backoff model
        '''
        return self.backoff
    
    def countWordsInFile(self, fileName):
        '''
        Counts how many words the given file contains
        '''
        
        counter = 0;
        input = open(fileName, "r");
          
        for line in input:
            words = line.strip().split()
            counter += len(words)    
        
        return counter
    
    def splitFile(self, fileName, percent, out1, out2):
        '''
        Splits the given input file according to the given percent
        '''
        
        totalLength = self.countWordsInFile(fileName) #calculates the length of input file
        
        input = open(fileName, "r") #open input
        output1 = open(out1, "w") #open output1
        output2 = open(out2, "w") #open output2
        
        counter = 0
    
        limit = int(percent * totalLength) #calculate the division
        
        #split the words to output files
        for line in input:
            words = line.strip().split()
            
            for word in words:
                
                if counter < limit:
                    print >> output1, word 
                else:
                    print >> output2, word 
               
                counter += 1
        
        
        input.close()
        output1.close()
        output2.close()
        
        return totalLength, limit, totalLength - limit    
    
    def createCorpuses(self,fileName):
        '''
        creates unigram and bigram corpuses from input file
        '''
        
        self.uniCorpus = Corpus(fileName)
        self.biCorpus = BigramCorpus(fileName, self.uniCorpus)
           
    def createConditioningDic(self, *fileName):
        '''
        for each x' the functions saves all the x's that are conditioned by it
        (for each 'the' save all 'boy's)  
        '''
        DEBUG("start mapping")
        
        #scan each word in corpus and accumulate its probability
        dic={}
        
        if len(fileName) == 0:
            outFile = open(conditionMap,"w")
            
            for w_tag, w_cndr in self.biCorpus.bigramDic.keys():     # for each w',w
                if dic.get(w_cndr) is None:
                    dic[w_cndr] = []
                    
                dic[w_cndr].append(w_tag)
                
            # for each w we got all w' s
                
            pickle.dump(dic, outFile)
            outFile.close()
        
        else:
            
            f = open (fileName[0],"r")
            dic = pickle.load(f)
            f.close()
        
        DEBUG( "stop debugging")
        return dic            
 